<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 调试打印
 */
function pr($data,$isExit = false){
    echo "<pre>";
    print_r($data);
    if( $isExit ) exit;
}

/**
 * 实例化数据模型：table（表名，不含前缀）
 */
function M($table=''){
    return \think\Db::name($table);
}

/**
 * 调用数据层模型
 */
function D($name,$layer='',$model='data'){
    if( $layer !== '' ){
        $layer = strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $layer), "_"));
        $layer = preg_replace("/_/", "\\", $layer);
        $layer = $layer.'\\';
    }
    $class = '\\'.$model.'\\'.$layer.$name;  
    return new \core\Loader($class);
}


/**
 * 抛出数据层的异常
 */
function dataE($message = '', $code = 0,$preException = ''){
    if( $preException instanceof \Exception ){
        throw new \jException\DataException($message,$code,$preException);
    }else{
        throw new \jException\DataException($message,$code);
    }
    
}

/**
 * 抛出接收到的参数校验异常
 */
function paramE($message = '', $code = 0,$preException = ''){
    if( $preException instanceof \Exception ){
        throw new \jException\ParamException($message,$code,$preException);
    }else{
        throw new \jException\ParamException($message,$code);
    }
}

/**
 * 抛出逻辑层的异常
 */
function logicE($message = '', $code = 0 , $preException = ''){
    if( $preException instanceof \Exception ){
        throw new \jException\LogicException($message,$code,$preException);
    }else{
        throw new \jException\LogicException($message,$code);
    }
}

/**
 * 抛出异常
 */
function E($tag='',$code=0 , $preException = ''){
    if( $preException instanceof \Exception ){
        throw new \think\Exception($tag,$code,$preException);
    }else{
        throw new \think\Exception($tag,$code);
    }
    
}

/**
 * 记录日志文件
 */
function writeLog($message,$level = 'log'){
    return \core\helper\Log::add($message,$level);
}


/**
 * 参数验证
 * @param $reportType 校验错误时处理方式 ： dataE 返回数据层异常, paramE 返回参数校验异常( 逻辑层 ) , list 返回全部校验结果
 */
function pVerify($check,$data,$reportType = 'paramE'){
    if( !in_array( $reportType ,[ 'dataE', 'paramE', 'list'] ) ) logicE('参数不合法');
    return \core\helper\Verify::checks($check,$data,$reportType);
}

/**
 * 获取过滤器对象
 */
function pFilter(){
    return \logic\sys\safe\InputFilter::getInstance();
}

/**
 * ip地址获取省市区地址
 */
function getIPArea($ip){
    $objIp = new \org\net\IpLocation;
    $ipArea = $objIp->getlocation($ip);
    $ipArea['country'] = iconv("gb2312","utf-8",$ipArea['country']);
    $ipArea['area'] = iconv("gb2312","utf-8",$ipArea['area']);
    return $ipArea;
}

/**
 * 抽取二维数组中的某个元素，组成新的一维数组
 */
function array_key_column($array=array(),$key){
    $data = array();
    foreach($array as $k=>$v){
        if(!isset($v[$key]))continue;
        $data[] = $v[$key];
    }
    return $data;
}